

var fs = require('fs');
var unzipmodule = require('unzip');
var path = require('path');

function topath(filename) {
	filename = filename.replace('.zip', '');
	var result = path.resolve(__dirname,'../storage/unzip/'+filename);
	return result;
}

function unzip(filename) {
	var frompath = path.resolve(__dirname,'../storage/zip/'+filename);
	var option = { path: topath(filename) };
	var unzipstrem = unzipmodule.Extract(option);
	return new Promise(function(resolve,reject){
		var readStream = fs.createReadStream(frompath);
		readStream.pipe(unzipstrem).on('close',function(){
			console.log('finish');
			resolve();
		});
	});

}

function testunzip() {
	unzip('wxapp.zip');
}

/**
{ fieldname: 'wxpack',
  originalname: 'src.zip',
  encoding: '7bit',
  mimetype: 'application/octet-stream',
  destination: '../storage/zip',
  filename: '03a44d40-93c1-11e7-bebf-f519f377866b.zip',
  path: '..\\storage\\zip\\03a44d40-93c1-11e7-bebf-f519f377866b.zip',
  size: 3581 }

*/



module.exports = unzip;